<?php
namespace classes\bank;
use classes\customer\Customer;
use utils\BankUtils;

class Bank
{
	const HOME_CREDIT = "HOME_CREDIT";
	const CONSUMER_CREDIT = "CONSUMER_CREDIT";
	
	private $name;
	private $address;
	private $bankoviProdukti = array(); // depoziti i krediti array
	private $countDeposit;
	private $countCredit;
	private $moneyInBank;
	private $bankovRezerv = 0;
	
	public function __construct($name,
								$address,
								$moneyInBank)
	{
		echo "<p>Bank: $name </p>";
		$this->setName($name);
		$this->setAddress($address);
		$this->setMoneyInBank($moneyInBank);
		$this->countDeposit = 0;
		$this->countCredit = 0;
				
	}
	// GET
	public function getName()
	{
		return $this->name;
	}
	public function getAddress()
	{
		return $this->address;
	}
	public function getBankoviProdukti()
	{
		return $this->bankoviProdukti;
	}
	public function getMoneyInBank()
	{
		return $this->moneyInBank;
	}
	// SET
	public function setName($name)
	{
		if($name == '' || $name == null)
		{
			throw new \Exception("Input name of class Bank!");
		}
		if(!is_string($name))
		{
			throw new \Exception("Wrong type for name of class Bank!");
		}
		$this->name = $name;
	}
	
	public function setAddress($address)
	{
		if($address == '' || $address == null)
		{
			throw new \Exception("Input address of class Bank!");
		}
		if(!is_string($address))
		{
			throw new \Exception("Wrong type for address of class Bank!");
		}
		$this->address = $address;
	}
	
	public function setMoneyInBank($moneyInBank)
	{
		if($moneyInBank == '' || $moneyInBank == null)
		{
			throw new \Exception("Input moneyInBank of class Bank!");
		}
		if(!is_numeric($moneyInBank))
		{
			throw new \Exception("Wrong type for moneyInBank of class Bank!");
		}
		if($moneyInBank < $this->bankovRezerv)
		{
			throw new \Exception("The Bank can't operate with les money then the Bank rezerve!");
		}
		$this->moneyInBank = $moneyInBank;
	}
	
	public function addDeposit(Deposit $deposit, Customer $customer)
	{
		if(!is_object($deposit))
		{
			throw new Exception("Deposit must be an object!");
		}
		if(!is_object($customer))
		{
			throw new Exception("Customer must be an object!");
		}
		if($customer->getMoney() - $deposit->getNalichnost() < 0){
			echo "<p>Sorry {$customer->getName()}, your money in cash are not enough!</p>"; 
		}
		else{
			$this->bankoviProdukti[] = $deposit;
			$customer->addDeposit($deposit);
			$customer->setMoney($customer->getMoney() - $deposit->getNalichnost());
			$this->moneyInBank += $deposit->getNalichnost();
			$this->bankovRezerv += ($deposit->getNalichnost() / 10);
			$this->countDeposit++;
			echo "<p>{$customer->getName()} has already {$customer->getMoney()} in cash! </p>";		
		}		
	}
	
	public function payLihviPoVsichkiDepoziti()
	{
		foreach ($this->bankoviProdukti as $value) {
		
			if(get_class($value) == "classes\bank\Deposit")
			{
				$value->setNalichnost($value->getNalichnost() + BankUtils::calculateDepositMontlyPayment($value->getNalichnost(), $value->getYearlyInterestRate()));		
			}
		}
		
	}
	
	private function addCredit(Credit $credit)
	{
		$this->bankoviProdukti[] = $credit;
	}
	
	public function giveCredit($number, $amount, $period, Customer $customer, $creditType)
	{
		
		$monthlyPayment = 0;
	
		foreach ($customer->getCredits() as $credit)
		{
			$monthlyPayment += $credit->getMonthlyPayment();
		}
		if($creditType == self::HOME_CREDIT)
		{
			$interestRate = 6;
				
		}else if($creditType == self::CONSUMER_CREDIT)
		{
			$interestRate = 10;
		}
		$monthlyPayment += BankUtils::calculateCreditMontlyPayment($amount, $interestRate, $period);
		if($monthlyPayment > ($customer->getSalary()/2))
		{
			throw new \Exception("You have reached the limit for your monthly payments!");
		}
		
		
		if($this->bankovRezerv > ($this->moneyInBank - $amount))
		{
			throw new \Exception("The bank reserve is not enough!");
		}
		
		if($creditType == self::HOME_CREDIT)
		{
			$clientCredit = new HomeCredit($number,  $period, $amount);
				
		}else if($creditType == self::CONSUMER_CREDIT)
		{
			$clientCredit = new ConsumerCredit($number, $period, $amount);
		}
		$clientCredit->showInfo();
		$customer->addCredit($clientCredit);
		$this->addCredit($clientCredit);
		$this->countCredit++;
	}
	
	public function showInfo(){
		
		echo "<b>Bank name: " . $this->name . "</b><br>" .
			 "Address: " . $this->address . "<br>" .
		     "Money in Bank: " . $this->moneyInBank . "lv<br>" .
			 "Bank reserv: "	. $this->bankovRezerv . "lv<br>" .
			 "Count deposits: " . $this->countDeposit . "<br>" .
			 "Count credits: " . $this->countCredit . "<br>";
	}
}






